package com.zbc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zbc.constant.StatusConstant;
import com.zbc.dto.CategoryDTO;
import com.zbc.dto.CategoryPageDTO;
import com.zbc.entity.Category;
import com.zbc.mapper.CategoryMapper;
import com.zbc.properties.JwtProperties;
import com.zbc.result.PageResult;
import com.zbc.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public PageResult getPageCategory(CategoryPageDTO categoryPageDTO) {
        PageHelper.startPage(categoryPageDTO.getPage(),categoryPageDTO.getPageSize());
        Page<Category> categories = categoryMapper.selectPageList(categoryPageDTO);
        PageResult pageResult = new PageResult();
        pageResult.setRecords(categories.getResult());
        pageResult.setTotal((int) categories.getTotal());

        return pageResult;
    }

    @Override
    public void switchCategoryStatus(Integer status, Integer id) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                .build();
        categoryMapper.update(category);
    }

    @Override
    public void saveCategory(CategoryDTO categoryDTO) {
        Category category = Category.builder()
                .name(categoryDTO.getName())
                .type(categoryDTO.getType())
                .sort(categoryDTO.getSort())
                .status(StatusConstant.DISABLE)
                .build();
        categoryMapper.insert(category);
    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = Category.builder()
                .id(categoryDTO.getId())
                .name(categoryDTO.getName())
                .type(categoryDTO.getType())
                .sort(categoryDTO.getSort())
                .build();
        categoryMapper.update(category);
    }

    @Override
    public void deleteCategory(Integer id) {
        categoryMapper.delete(id);
    }

    @Override
    public Category[] fetchTypeList(Integer type) {
        return categoryMapper.fetchTypeList(type);
    }


}
